<?php

namespace app\pay\command;

use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class GetPrice extends Command
{
    protected function configure()
    {
        $this->setName('GetPrice')
            ->setDescription('usdt-cny');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/
        //获取支付宝,微信,银行卡价格
        $alipay = $this->getCurl("aliPay");
        if (isset($alipay['data'][0]['price'])) {
            $price['alipay'] = $alipay['data'][0]['price'];
        } else {
            $price['alipay'] = 6.2;
        }
        $wxpay = $this->getCurl("wxPay");
        if (isset($wxpay['data'][0]['price'])) {
            $price['wxpay'] = $wxpay['data'][0]['price'];
        } else {
            $price['wxpay'] = 6.2;
        }
        $bank = $this->getCurl("bank");
        if (isset($bank['data'][0]['price'])) {
            $price['bank'] = $bank['data'][0]['price'];
        } else {
            $price['bank'] = 6.2;
        }
        $redis = new Redis();
        $site_info['alipay'] = $redis->set("alipay", $price['alipay']);
        $site_info['wxpay'] = $redis->set("wxpay", $price['wxpay']);
        $site_info['bank'] = $redis->set("bank", $price['bank']);
        /*** 这里写计划任务列表集 END ***/
        $output->writeln(implode(",", $price));
    }

    public function getCurl($param)
    {
        $time = time() * 1000;
        $url = "https://www.okex.com/v3/c2c/otc-ticker/quotedPrice?t=" . $time . "&baseCurrency=USDT&quoteCurrency=CNY&side=buy&paymentMethod=" . $param;
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                'Cookie: locale=en_US'
            ),
        ));
        $response = curl_exec($curl);
        if(curl_exec($curl) === false) {
            echo 'Curl error: ' . curl_error($curl);
        }
        curl_close($curl);
        $response = json_decode($response, true);
        return $response;
    }

}
